package com.jin.webvideo.controller;

import com.jin.webvideo.exception.ParsingFailedException;
import com.jin.webvideo.parser.ParseResult;
import com.jin.webvideo.service.ParserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 电视家自定义频道解析
 */
@Controller()
@RequestMapping("/newlive")
public class NewLiveController {
    @Autowired
    private ParserService parserService;

    @RequestMapping(path = "/{platform}/{roomId}")
    public void newlive(HttpServletRequest request, HttpServletResponse response, @PathVariable("platform") String platform, @PathVariable("roomId")String roomid) {
        try {
            ParseResult url = parserService.scriptParser(request, roomid, "", platform);
            if ("hls".equals(url.getType())) {
                response.setStatus(301);
                response.setHeader("location", url.getUrl());
                return;
            }
        } catch (ParsingFailedException e) {
            e.printStackTrace();
        }
        response.setStatus(404);
    }
}
